<template>
  <!--list-->
  <!--@author: 梁凤波-->
  <!--@date  2018/11/22 22:16-->
  <section class="list-wrap">
    <div class="list" v-if="list.length > 0">
      <Table border :columns="columns" :data="list"></Table>
    </div>
  </section>
</template>

<script>
  import {mapState, mapActions} from 'vuex';

  export default {
    data() {
      return {
        list: [],
        columns: [
          {
            title: 'ID',
            key: 'id',
            width: 120,
            align: 'center'
          },
          {
            title: '用户名',
            key: 'username'
          },
          {
            title: '邮箱',
            key: 'email'
          },
          {
            title: '创建日期',
            key: 'created_id'
          },
          {
            title: '更新日期',
            key: 'updated_id'
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.update(params.row.id);
                    }
                  }
                }, '修改'),
              ]);
            }
          }
        ],
      }
    },
    computed: {
      ...mapState({})
    },
    created() {
      this.getUserList()
    },
    methods: {
      ...mapActions({
        userList: 'user/userList'
      }),
      // 获取用户列表
      async getUserList() {
        try {
          const ret = await this.userList();
          if(localStorage.username == 'admin'){
            this.list = ret.data.data;
          }else{
            this.list = ret.data.data.filter(item =>{
              return item.username == localStorage.username
            })
          }

        } catch (e) {

        }
      },
      update(id) {
        this.$router.push("/user/update/" + id);
      },
      remove(index) {
        console.log(index);
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
